package com.example.springboottestgit.controller.main;

import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: ZZZ
 */
public class VideoMain {
    public static void main(String[] args) throws IOException {
        convertImagesToVideo();
    }

    public static void convertImagesToVideo() throws IOException {

        LocalDateTime s = LocalDateTime.now();

        //视频尺寸

        //视频输出  图片路径
        String imageDir = "D:\\ZZZ\\2\\video\\";
        String outputPath = "D:\\ZZZ\\2\\video\\30帧.mp4";


        File outputFile = new File(outputPath);

        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputFile, 1280, 720);
        recorder.setVideoCodec(27);
        recorder.setFrameRate(30);
        recorder.setVideoBitrate(12000000);
        recorder.start();


        List<BufferedImage> images = new ArrayList<>();
        Java2DFrameConverter converter = new Java2DFrameConverter();

        File[] files = new File(imageDir).listFiles();
        Arrays.sort(files, (f1, f2) -> f1.getName().compareTo(f2.getName()));

        for (File file : files) {
            if (file.getName().endsWith(".jpg") || file.getName().endsWith(".png")) {
                System.out.println(file.getName());
                BufferedImage image = ImageIO.read(file);
                Frame frame = converter.convert(image);
                recorder.record(frame);
            }
        }

        recorder.stop();
        recorder.release();
        LocalDateTime e = LocalDateTime.now();
        Duration between = Duration.between(s, e);


    }

}
